package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.domain.service.IOperationsService;
import com.hyzx.qbasic.admin.domain.service.ISysDataSetService;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.model.po.SysDataSetPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.model.vo.res.admin.GetDataSetDataRes;
import com.hyzx.qbasic.common.util.StringParserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.Map;

/**
 * 数据集管理 控制器类
 *
 * @author 黄江华
 * @since 2019-04-09
 */

@Api(tags = "数据集管理", description = "SysDataSetController")
@Controller
@RequestMapping("/sys/dataset")
public class SysDataSetController extends BaseController {
    /**
     * 获取数据集数据每次最多返回100条记录
     */
    private static final int MAX_ROWS_COUNT = 100;

    @Autowired
    private ISysDataSetService sysDataSetService;

    @Autowired
    private IOperationsService operationsService;

    @ApiIgnore
    @SysLog("数据集管理首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_DATASET_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_DATASET_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_DATASET_DELETE));
        return "system/dataset/list";
    }

    @ApiOperation("获取数据集管理列表")
    @GetMapping("/getSysDataSetList")
    @ResponseBody
    public BaseResModel<PageInfo<SysDataSetPO>> getSysDataSetList(@Valid GetSysDataSetListReq req, BindingResult result) {
        return BaseResUtils.success(sysDataSetService.getPagedSysDataSetList(req));
    }

    @ApiIgnore
    @SysLog("新增数据集管理页")
    @GetMapping("/add")
    public String add() {
        return "system/dataset/add";
    }

    @ApiOperation("新增数据集管理表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddSysDataSetReq req, BindingResult result) {
        BaseOperationRes res = sysDataSetService.addSysDataSet(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除数据集管理记录")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteSysDataSetReq req, BindingResult result) {
        BaseOperationRes res = sysDataSetService.deleteSysDataSet(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑数据集管理页")
    @GetMapping("/edit")
    public String edit() {
        return "system/dataset/edit";
    }

    @ApiOperation("编辑数据集管理表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditSysDataSetReq req, BindingResult result) {
        BaseOperationRes res = sysDataSetService.editSysDataSet(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取数据集管理信息")
    @GetMapping("/getSysDataSetInfo")
    @ResponseBody
    public BaseResModel<SysDataSetPO> getSysDataSetInfo(@Valid GetSysDataSetInfoReq req, BindingResult result) {
        return BaseResUtils.success(sysDataSetService.getSysDataSetInfoById(req));
    }

    @ApiOperation("获取数据集数据")
    @PostMapping("/getDataSetData")
    @ResponseBody
    public BaseResModel<GetDataSetDataRes> getDataSetData(@Valid GetSysDataSetDataReq req, @RequestBody Map<String, String> params, BindingResult result) {
        SysDataSetPO sysDataSetPO = sysDataSetService.getSysDataSetInfoByName(req.getDatasetName());
        String sql = sysDataSetPO.getSqlString();

        if (params != null && !params.isEmpty()) {
            sql = StringParserUtils.parseDollarMark(sql, params);
        }

        DbQueryReq dbQueryReq = new DbQueryReq();
        dbQueryReq.setSql(sql);
        dbQueryReq.setPageNum(1);
        dbQueryReq.setPageSize(MAX_ROWS_COUNT);

        PageInfo<Map<String, Object>> data = operationsService.dbQuery(dbQueryReq);
        if (data == null)
            return BaseResUtils.paramError("数据库查询异常，SQL语法不正确");

        GetDataSetDataRes res = new GetDataSetDataRes();
        res.setData(data.getList());
        if (data.getList().isEmpty())
            res.setCols(Collections.EMPTY_SET);
        else
            res.setCols(data.getList().get(0).keySet());

        return BaseResUtils.success(res);
    }
}